<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of utama
 *
 * @author Eko Prasetyo<nir@nirleka.web.id>
 */
class utama extends GON_User {

	function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	}

	function __recursive_remove_directory($directory, $empty=FALSE)
	{
		// if the path has a slash at the end we remove it here
		if(substr($directory,-1) == '/')
		{
			$directory = substr($directory,0,-1);
		}

		// if the path is not valid or is not a directory ...
		if(!file_exists($directory) || !is_dir($directory))
		{
			// ... we return false and exit the function
			return FALSE;

		// ... if the path is not readable
		}elseif(!is_readable($directory))
		{
			// ... we return false and exit the function
			return FALSE;

		// ... else if the path is readable
		}else{

			// we open the directory
			$handle = opendir($directory);

			// and scan through the items inside
			while (FALSE !== ($item = readdir($handle)))
			{
				// if the filepointer is not the current directory
				// or the parent directory
				if($item != '.' && $item != '..')
				{
					// we build the new path to delete
					$path = $directory.'/'.$item;

					// if the new path is a directory
					if(is_dir($path))
					{
						// we call this function with the new path
						$this->__recursive_remove_directory($path);

					// if the new path is a file
					}else{
						// we remove the file
						unlink($path);
					}
				}
			}
			// close the directory
			closedir($handle);

			// if the option to empty is not set to true
			if($empty == FALSE)
			{
				// try to delete the now empty directory
				if(!rmdir($directory))
				{
					// return false if not possible
					return FALSE;
				}
			}
			// return success
			return TRUE;
		}
	}

	function index() {
		$this->daftar_proyek();
	}

	function daftar_proyek() {
		$data['title'] = "Daftar Proyek";
		$data['id'] = 1;
		$data['type'] = 'daftar_proyek';
		if ( (strcmp($this->session->userdata('jabatan'), 'Admin') != 0) &&
			 (strcmp($this->session->userdata('jabatan'), 'Direktur') != 0) &&
			 (strcmp($this->session->userdata('jabatan'), 'Sekretaris') != 0) &&
			 (strcmp($this->session->userdata('jabatan'), 'Direktur utama') != 0) &&
			 (strcmp($this->session->userdata('jabatan'), 'Direktur teknik') != 0)	)
		{
			$data['daftar_proyek'] = $this->db
										   ->distinct()
										   ->select('idRuangProyek, nama_proyek, pemilik_proyek, tanggal_mulai, tanggal_deadline, status')
										   ->from('ruang_proyek, pegawai_ruang_proyek')
										   ->where('idPegawai', $this->session->userdata('idPegawai'))
										   ->where('idruang_proyek = idRuangProyek')
										   ->get()
										   ->result_array();
		}
		else
		{
			$data['daftar_proyek'] = $this->db
										   ->distinct()
										   ->select('idRuangProyek, pemilik_proyek, nama_proyek, tanggal_mulai, tanggal_deadline, status')
										   ->from('ruang_proyek, pegawai_ruang_proyek')
										   ->get()
										   ->result_array();
		}
		$this->__declare_view($data, "daftar_proyek");
	}

	function daftar_pegawai() {
		$data['title'] = "Daftar Pegawai";
		$data['id'] = 2;
		$data['type'] = 'daftar_pegawai';
		$data['daftar_pegawai'] = $this->db->select('idPegawai, nama_pegawai')->from('pegawai')->get()->result_array();
		$this->__declare_view($data, "daftar_pegawai");
	}

	function create_proyek() {
		$data['title'] = "Create Proyek";
		$data['id'] = 1;
		$data['type'] = 'create_proyek';
		$data['pegawai'] = $this->db
								->select('nama_pegawai,idPegawai')
								->from('pegawai')
								->where_not_in('jabatan', 'admin')
								->get()
								->result_array();
		$this->__declare_view($data, "create_proyek");
	}

	function create_pegawai() {
		$data['title'] = "Create Pegawai";
		$data['id'] = 2;
		$data['type'] = 'create_pegawai';
		$this->__declare_view($data, "create_pegawai");
	}

	function update_proyek($id) {
		$data['title'] = "Update Proyek";
		$data['id'] = 1;
		$data['type'] = 'update_proyek';
		$data['pegawai_proyek'] = array();
		$data['proyek'] = $this->db->from('ruang_proyek')->where('idRuangProyek', $id)->get()->row_array();
		$temp = $this->db->select('idPegawai')->from('pegawai_ruang_proyek')->where('idruang_proyek', $id)->get()->result_array();
		foreach ($temp as $row) {
			array_push($data['pegawai_proyek'], $row['idPegawai']);
		}
		$data['daftar_pegawai'] = $this->db->select('idPegawai, nama_pegawai')->from('pegawai')->get()->result_array();
		$this->__declare_view($data, "update_proyek");
	}

	function update_pegawai($id) {
		$data['title'] = "Update Pegawai";
		$data['id'] = 2;
		$data['type'] = 'update_pegawai';
		$data['pegawai'] = $this->db->from('pegawai')->where('idPegawai', $id)->get()->row_array();
		$this->__declare_view($data, "update_pegawai");
	}

	function delete_proyek($id) {
		$del = $this->db->select('nama_proyek')->where('idRuangProyek', $id)->from('ruang_proyek')->get()->row_array();
		$this->db->where('idRuangProyek', $id);
		$this->db->delete('ruang_proyek');
		$del['nama_proyek'] = str_replace(" ", "_", $del['nama_proyek']);
		$project = './project/' . $del['nama_proyek'];
		$this->__recursive_remove_directory($project);
		$this->flash_message->success("Proyek berhasil dihapus");
		redirect("utama/daftar_proyek");
	}

	function delete_pegawai($id) {
		$this->db->where('idPegawai', $id);
		$this->db->delete('pegawai');
		$this->flash_message->success("Pegawai berhasil dihapus");
		redirect("utama/daftar_pegawai");
	}

	function view_pegawai($id) {
		$data['title'] = "Lihat Pegawai";
		$data['id'] = 2;
		$data['type'] = 'lihat_pegawai';
		$data['data_pegawai'] = $this->db->from('pegawai')->where('idPegawai', $id)->get()->row_array();
		$this->__declare_view($data, "lihat_pegawai");
	}

	function view_proyek($id) {
		$data['title'] = "Lihat Proyek";
		$data['id'] = 1;
		$data['type'] = 'lihat_proyek';
		$data['folder'] = str_replace("-", "_", $id);
		$id = str_replace("-", " ", $id);
		$data['proyek'] = $this->db->from('ruang_proyek')->where('nama_proyek', $id)->get()->row_array();
		$this->__declare_view($data, "lihat_proyek");
	}

	function save_proyek() {
		$this->__check_login();
		$data = $this->input->post();
		$proyek = array(
			'nama_proyek' => $data['nama_proyek'],
			'pemilik_proyek' => $data['pemilik_proyek'],
			'penanggung_jawab' => $data['penanggung_jawab'],
			'total_dana' => $data['total_dana'],
			'tanggal_mulai' => $data['tanggal_mulai'],
			'tanggal_deadline' => $data['tanggal_deadline'],
			'status' => 'dikerjakan'
		);
		$this->db->insert('ruang_proyek', $proyek);
		$idProyek = $this->db->select('idRuangProyek')->from('ruang_proyek')->where('nama_proyek', $proyek['nama_proyek'])->get()->row_array();
		$folder = str_replace(" ", "_", $proyek['nama_proyek']);
		mkdir('project/' . $folder);
		mkdir('project/' . $folder . '/' . 'interior');
		mkdir('project/' . $folder . '/' . 'client');
		mkdir('project/' . $folder . '/' . 'estetika');
		mkdir('project/' . $folder . '/' . 'arsitektur');
		mkdir('project/' . $folder . '/' . 'draft');
		foreach ($data['idPegawai'] as $row) {
			$data2 = array(
				'idPegawai' => $row,
				'idruang_proyek' => $idProyek['idRuangProyek']
			);
			$this->db->insert('pegawai_ruang_proyek', $data2);
		}
		$this->flash_message->success("Proyek berhasil disimpan");
		redirect('utama/daftar_proyek');
	}

	function save_pegawai() {
		$this->__check_login();
//		$rule = array(
//               array(
//                     'field'   => 'username',
//                     'label'   => 'Username',
//                     'rules'   => 'required|max_length[45]|min_length[5]'
//                  ),
//               array(
//                     'field'   => 'password',
//                     'label'   => 'Password',
//                     'rules'   => 'required|max_length[45]|min_length[5]'
//                  ),
//			   array(
//                     'field'   => 'nama_pegawai',
//                     'label'   => 'Nama Pegawai',
//                     'rules'   => 'required|max_length[45]|min_length[5]'
//                  ),
//               array(
//                     'field'   => 'alamat',
//                     'label'   => 'Alamat',
//                     'rules'   => 'required|max_length[45]|min_length[5]'
//                  ),
//               array(
//                     'field'   => 'email',
//                     'label'   => 'Email',
//                     'rules'   => 'required|valid_email|is_unique[users.email]'
//                  )
//            );
//		$this->form_validation->set_rules($rule);
//		if ($this->form_validation->run() == true) {
		$data = $this->input->post();
		$this->db->insert('pegawai', $data);
		$this->flash_message->success("Pegawai berhasil disimpan");
		redirect('utama/daftar_pegawai');
	}

	function proses_update_pegawai() {
		if (!empty($_POST['submit'])) {
			$data_lama = $this->db->from('pegawai')->where('idPegawai', $_POST['id_pegawai'])->get()->row_array();
			$data_baru = array(
				'username' => $this->input->post('username'),
				'password' => $this->input->post('password'),
				'nama_pegawai' => $this->input->post('nama_pegawai'),
				'alamat' => $this->input->post('alamat'),
				'tanggal_lahir' => $this->input->post('tanggal_lahir'),
				'email' => $this->input->post('email'),
				'no_telpon' => $this->input->post('no_telpon')
			);
			$c = array_diff_assoc($data_baru, $data_lama);
			$this->db->where('idPegawai', $_POST['id_pegawai'])->update('pegawai', $c);
			$this->flash_message->success("Data Pegawai berhasil diubah");
		} else {
			$this->flash_message->error("Data Pegawai tidak boleh kosong");
		}
		redirect('utama/update_pegawai/' . $_POST['id_pegawai']);
	}

	function proses_update_proyek() {
		if (!empty($_POST['submit_update_proyek'])) {
			//ambil data proyek
			$data_lama_proyek = $this->db->from('ruang_proyek')->where('idRuangProyek', $_POST['idRuangProyek'])->get()->row_array();
			$data_baru_proyek = array(
				'nama_proyek' => $this->input->post('nama_proyek'),
				'pemilik_proyek' => $this->input->post('pemilik_proyek'),
				'penanggung_jawab' => $this->input->post('penanggung_jawab'),
				'total_dana' => $this->input->post('total_dana'),
				'tanggal_mulai' => $this->input->post('tanggal_mulai'),
				'tanggal_deadline' => $this->input->post('tanggal_deadline'),
				'status' => $this->input->post('status')
			);

			//update data proyek
			$c = array_diff($data_baru_proyek, $data_lama_proyek);
			if (!empty($c)) {
				$this->db->where('idRuangProyek', $_POST['idRuangProyek'])->update('ruang_proyek', $c);
				$this->flash_message->success('Data proyek berhasil diubah');
			}

			//update data pegawai yang mengikuti proyek
			//data pegawai yang baru diimput untuk mengikuti proyek
			$data_baru = array();
			//hapus semua data lama
			$this->db->where('idruang_proyek', $_POST['idRuangProyek'])->delete('pegawai_ruang_proyek');

			if (!empty($_POST['idPegawai'])) {
				$i = 0;
				foreach ($this->input->post('idPegawai') as $row) {
					$data_baru[$i]['idPegawai'] = $row;
					$data_baru[$i]['idruang_proyek'] = $_POST['idRuangProyek'];
					$i++;
				}
				//insert semua data
				$this->db->insert_batch('pegawai_ruang_proyek', $data_baru);
			}
			else {
				$this->flash_message->success("Data pegawai berhasil dihapus");
			}
			redirect('utama/update_proyek/' . $_POST['idRuangProyek']);
		}
	}

}
//end of file